package com.jerry.walking.module.activityPath.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.walking.R;
import com.jerry.walking.base.BaseApplication;
import com.jerry.walking.base.BaseFragment;
import com.jerry.walking.base.BaseViewHolder;
import com.jerry.walking.base.MyBaseAdapter;
import com.jerry.walking.constant.Urls;
import com.jerry.walking.module.activityPath.activity.SearchActivity;
import com.jerry.walking.module.activityPath.bean.PathRouteBase1;
import com.jerry.walking.utils.OkHttpUtils;
import com.jerry.walking.views.ExpandGridView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yang on 2016/5/13.
 *
 *          路线 --->   路线库主界面
 */
public class RouteBaseFragment extends BaseFragment {

    @BindView(R.id.lv_routebase)
    PullToRefreshListView listView;

    private MyBaseAdapter<PathRouteBase1.RegionsBean> adapter;

    private List<PathRouteBase1.RegionsBean> items;

    private ImageLoader imageLoader;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_path_routebase;
    }

    @Override
    protected void initViews() {
        super.initViews();

        imageLoader = BaseApplication.getApplication().getImageLoader();

        items = new ArrayList<>();

        listView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        adapter = new MyBaseAdapter<PathRouteBase1.RegionsBean>(RouteBaseFragment.this.getActivity(), items) {
            @Override
            public int getLayoutId() {
                return R.layout.adapter_path_routebase;
            }

            @Override
            public BaseViewHolder getViewHolder() {
                return new ViewHolder();
            }

            @Override
            public void bindViews(View view, BaseViewHolder viewHolder) {
                final ViewHolder holder = (ViewHolder) viewHolder;

                holder.textView.setText(items.get(holder.position).getRegion());

                holder.gridView.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return items.get(holder.position).getProvince().size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return null;
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }

                    @Override
                    public View getView(int index, View convertView, ViewGroup parent) {

                        View inflate = View.inflate(RouteBaseFragment.this.getActivity(), R.layout.adapter_path_routebase_gridview, null);

                        ImageView imageView = (ImageView) inflate.findViewById(R.id.iv_two);
                        TextView textView = (TextView) inflate.findViewById(R.id.tv_two);

                        DisplayImageOptions options = new DisplayImageOptions.Builder()
                                .cacheOnDisk(true)
                                .cacheInMemory(true)
                                .imageScaleType(ImageScaleType.EXACTLY)
                                .build();
                        imageLoader.displayImage(items.get(holder.position).getProvince().get(index).getIcon(), imageView, options);
                        textView.setText("路线"+items.get(holder.position).getProvince().get(index).getTrail_count());

                        return inflate;
                    }
                });

                //点击事件跳转搜索界面
                holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                        Intent intent = new Intent(RouteBaseFragment.this.getActivity(), SearchActivity.class);
                        intent.putExtra("name", items.get(holder.position).getProvince().get(i).getName());
                        startActivity(intent);
                    }
                });
            }
        };
        listView.setAdapter(adapter);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                items.clear();
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
    }

    @Override
    protected void loadData() {
        super.loadData();

        OkHttpUtils.get(Urls.PATH_ROUTEBASE, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {

                Gson gson = new Gson();

                PathRouteBase1 pathRouteBase1 = gson.fromJson(data, PathRouteBase1.class);

                List<PathRouteBase1.RegionsBean> regions = pathRouteBase1.getRegions();

                items.addAll(regions);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });

    }


    class ViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_path_routebase_adapter)
        TextView textView;

        @BindView(R.id.gv_path_routebase_adapter)
        ExpandGridView gridView;
    }
}
